package com.smart4s.app.core.positionservice.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.smart4s.app.core.positionservice.activity.MainActivity;
import com.smart4s.app.core.positionservice.service.LightDecoderService;

/*
 * Copyright (C)
 * <p>
 * FileName: BootBroadcastReceiver
 * <p>
 * Author: zhaozhipeng
 * <p>
 * Date: 2020/5/22 12:49
 * <p>
 * description：开机自启Service
 **/

public class BootBroadcastReceiver extends BroadcastReceiver {
    public final static String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_BOOT_COMPLETED)) {
            Intent intent1 = new Intent(context, MainActivity.class);
            intent1.setAction("android.intent.action.MAIN");
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent1);
            Log.e("BroadcastReceiver", "BOOT 开机自动启动");
            //启动服务的地方
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(new Intent(context, LightDecoderService.class));
            } else {
                context.startService(new Intent(context, LightDecoderService.class));
            }
        }
    }

}
